package org.jugile.jsontmpl.domain;

import java.util.ArrayList;
import java.util.List;

import org.jugile.jsontmpl.common.*;
import org.jugile.jsontmpl.domain.ObjBase;
import org.jugile.util.Node;
import org.jugile.util.Proxy;
import org.jugile.util.Vo;

public class Obj extends ObjBase implements org.jugile.util.IDO {

	public Fld createFld(String name) {
		return d().createFld().setName(name).setObj(this);
	}
	public Fld getOrCreateFld(String name) {
		Fld res = getFlds().byName(name).unique();
		if (res != null) return res;
		return createFld(name);
	}

	public Value set(String name, String str) {
		return getOrCreateFld(name).createValue().setString(str);
	}
	public Value set(String name, Number num) {
		return getOrCreateFld(name).createValue().setNumber(num);
	}
	public Value set(String name, Obj o) {
		return getOrCreateFld(name).createValue().setO(o);
	}
	public Value set(String name, Array2 a) {
		return getOrCreateFld(name).createValue().setArr(a);
	}
	
	
	public Node toXml() {
		Node n = new Node("obj");
		if (!empty(getName())) n.attr("name", getName()); 
		if (!empty(getCall())) n.attr("c", getCall());
		if (!empty(getDesc())) n.set("desc", getDesc()); 
		for (Fld fld : getFlds().sort("id")) n.add(fld.toXml());
		for (Include incl : getIncludes().sort("id")) n.add(incl.toXml());
		return n;
	}
	
	public Vo toVo() {
		return toVo(0);
	}
	public Vo toVo(int idx) {
		Vo v = new Vo();
		for (Fld fld : getFlds().sort("id")) v.set(fld.getName(), fld.toVo(idx));
		return v;
	}
	
	protected String[] params() {
		List<String> res = new ArrayList<String>();
		if (res.size() == 0) return null;
		String arr[] = new String[res.size()];
		for (int i = 0; i < res.size(); i++) arr[i] = res.get(i);
		return arr;
	}
	
	public Vo toVo(EvalCtx ctx) {
		Vo v = new Vo();
		if (!empty(getCall())) {
			if (ctx.pushCall(getCall(),params()) == null) return null; 
		}
		for (Fld fld : getFlds().sort("id")) {
			if (fld.isVisible(ctx))
				v.set(fld.getName(), fld.toVo(ctx));
		}
		if (!empty(getCall())) ctx.pop();
		return v;
	}
	
	public Obj parse(Ctx ctx, Node n) {
		if (!"obj".equals(n.name())) fail("element must be obj");
		
		n.validateElements("desc fld include");
		n.validateAttributes("c p1 p2 include name");
		
		setCall(n.attr("c"));
		
		String include = n.attr("include");
		if (!empty(include)) {
			Obj o = d().parseXmlFile(ctx, include);
			if (o == null) return null;
			if (!empty(getCall())) o.setCall(getCall());
			delete(); // COOL
			return o;
		}
		
		setName(n.attr("name"));
		setDesc(n.str("desc"));
		for (Node f : n.list("fld")) {
			Fld fld = createFld(f.attr("name"));
			fld.parse(ctx,f);
		}
		
		for (Node incl : n.list("include")) {
			incl.validateAttributes("file flds");
			createInclude().parse(ctx,incl);
			Obj o = d().parseXmlFile(ctx,incl.attr("file"));
			if (o == null) return null;
			// copy flds
			for (Fld fld2 : o.getFlds().byMatchFilter(incl.attr("flds"))) {
				addFld(fld2);
			}
		}
		return this;
	}
	
	public Include createInclude() {
		return d().createInclude().setObj(this);
	}
	

	public void onDelete() {
		for (Fld fld : getFlds()) fld.delete();
		for (Include i : getIncludes()) i.delete();
	}

	public boolean isRootObj() {
		if (!empty(getFname())) return true;
		return false;
	}
}